package com.p.lib.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;

import com.heartware.sh.commonlib.R;
import com.p.lib.app.BaseApplication;

/**
 * 日期工具类
 * @author ChenNan
 *
 */
@SuppressLint("SimpleDateFormat") public class DateUtil {

	/**
	 * 时间日期格式化到年月日 中文间隔
	 * yyyy年MM月dd日
	 */
	public static String dateFormatYMD_CN = "yyyy年MM月dd日";

	/**
	 * 时间日期格式化到年月日时分 中文间隔
	 * yyyy年MM月dd日 HH:mm
	 */
	public static String dateFormatYMDHM_CN = "yyyy年MM月dd日 HH:mm";

	/**
	 * 时间日期格式化到年月日时分 冒号间隔
	 * yyyy:MM:dd HH:mm
	 */
	public static String dateFormatYMDHM_COLON = "yyyy:MM:dd HH:mm";

	/**
	 * 时间日期格式化到年月日时分秒 中杠间隔
	 * yyyy-MM-dd HH:mm:ss
	 */
	public static String dateFormatYMDHMS = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 时间日期格式化到年月日时分
	 */
	public static String dateFormatYMDHM = "yyyy-MM-dd HH:mm";

	/**
	 * 时间日期格式化到年月日
	 * yyyy-MM-dd
	 */
	public static String dateFormatYMD = "yyyy-MM-dd";

	/**
	 * 时间日期格式化到年月
	 * yyyy-MM
	 */
	public static String dateFormatYM = "yyyy-MM";


	/** 时间日期格式化到月日. */
	public static String dateFormatMD = "MM-dd";

	/** 时间日期格式化到月日  CN. */
	public static String dateFormatMD_CN = "MM月dd日";

	/**
	 * 时分秒
	 * HH:mm:ss
	 */
	public static String dateFormatHMS = "HH:mm:ss";

	/** 
	 * 时分
	 * HH:mm
	 */
	public static String dateFormatHM = "HH:mm";
	
	/** 
	 * 分秒
	 * mm:ss
	 */
	public static String dateFormatMS = "mm:ss";

	/**
	 * 描述：String类型的日期时间转化为Date类型.
	 * 
	 * @param strDate
	 *            String形式的日期时间
	 * @param format
	 *            格式化字符串，如："yyyy-MM-dd HH:mm:ss"
	 * @return Date Date类型日期时间
	 */
	public static Date getDateByFormat(String strDate, String format) {
		SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = mSimpleDateFormat.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 描述：获取偏移之后的Date.
	 * 
	 * @param date
	 *            日期时间
	 * @param calendarField
	 *            Calendar属性，对应offset的值，
	 *            如(Calendar.DATE,表示+offset天,Calendar.HOUR_OF_DAY,表示＋offset小时)
	 * @param offset
	 *            偏移(值大于0,表示+,值小于0,表示－)
	 * @return Date 偏移之后的日期时间
	 */
	public Date getDateByOffset(Date date, int calendarField, int offset) {
		Calendar c = new GregorianCalendar();
		try {
			c.setTime(date);
			c.add(calendarField, offset);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c.getTime();
	}

	/**
	 * 描述：获取指定日期时间的字符串(可偏移).
	 * 
	 * @param strDate
	 *            String形式的日期时间
	 * @param format
	 *            格式化字符串，如："yyyy-MM-dd HH:mm:ss"
	 * @param calendarField
	 *            Calendar属性，对应offset的值，
	 *            如(Calendar.DATE,表示+offset天,Calendar.HOUR_OF_DAY,表示＋offset小时)
	 * @param offset
	 *            偏移(值大于0,表示+,值小于0,表示－)
	 * @return String String类型的日期时间
	 */
	public static String getStringByOffset(String strDate, String format,
			int calendarField, int offset) {
		String mDateTime = null;
		try {
			Calendar c = new GregorianCalendar();
			SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
			c.setTime(mSimpleDateFormat.parse(strDate));
			c.add(calendarField, offset);
			mDateTime = mSimpleDateFormat.format(c.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return mDateTime;
	}

	/**
	 * 描述：Date类型转化为String类型(可偏移).
	 * 
	 * @param date
	 *            the date
	 * @param format
	 *            the format
	 * @param calendarField
	 *            the calendar field
	 * @param offset
	 *            the offset
	 * @return String String类型日期时间
	 */
	public static String getStringByOffset(Date date, String format,
			int calendarField, int offset) {
		String strDate = null;
		try {
			Calendar c = new GregorianCalendar();
			SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
			c.setTime(date);
			c.add(calendarField, offset);
			strDate = mSimpleDateFormat.format(c.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strDate;
	}

	/**
	 * 描述：Date类型转化为String类型.
	 * 
	 * @param date
	 *            the date
	 * @param format
	 *            the format
	 * @return String String类型日期时间
	 */
	public static String getStringByFormat(Date date, String format) {
		SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
		String strDate = null;
		try {
			strDate = mSimpleDateFormat.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strDate;
	}

	/**
	 * 描述：获取指定日期时间的字符串,用于导出想要的格式.
	 * 
	 * @param strDate
	 *            String形式的日期时间，必须为yyyy-MM-dd HH:mm:ss格式
	 * @param format
	 *            输出格式化字符串，如："yyyy-MM-dd HH:mm:ss"
	 * @return String 转换后的String类型的日期时间
	 */
	public static String getStringByFormat(String strDate, String format) {
		String mDateTime = null;
		try {
			Calendar c = new GregorianCalendar();
			SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(
					dateFormatYMDHMS);
			c.setTime(mSimpleDateFormat.parse(strDate));
			SimpleDateFormat mSimpleDateFormat2 = new SimpleDateFormat(format);
			mDateTime = mSimpleDateFormat2.format(c.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mDateTime;
	}

	/**
	 * 描述：获取milliseconds表示的日期时间的字符串.
	 * 
	 * @param format
	 *            格式化字符串，如："yyyy-MM-dd HH:mm:ss"
	 * @return String 日期时间字符串
	 */
	public static String getStringByFormat(long milliseconds, String format) {
		String thisDateTime = null;
		try {
			SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
			thisDateTime = mSimpleDateFormat.format(milliseconds);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return thisDateTime;
	}

	/**
	 * 描述：获取表示当前日期时间的字符串.
	 * 
	 * @param format
	 *            格式化字符串，如："yyyy-MM-dd HH:mm:ss"
	 * @return String String类型的当前日期时间
	 */
	public static String getCurrentDate(String format) {
		String curDateTime = null;
		try {
			SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
			Calendar c = new GregorianCalendar();
			curDateTime = mSimpleDateFormat.format(c.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return curDateTime;

	}

	/**
	 * 描述：获取表示当前日期时间的字符串(可偏移).
	 * 
	 * @param format
	 *            格式化字符串，如："yyyy-MM-dd HH:mm:ss"
	 * @param calendarField
	 *            Calendar属性，对应offset的值，
	 *            如(Calendar.DATE,表示+offset天,Calendar.HOUR_OF_DAY,表示＋offset小时)
	 * @param offset
	 *            偏移(值大于0,表示+,值小于0,表示－)
	 * @return String String类型的日期时间
	 */
	public static String getCurrentDateByOffset(String format,
			int calendarField, int offset) {
		String mDateTime = null;
		try {
			SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
			Calendar c = new GregorianCalendar();
			c.add(calendarField, offset);
			mDateTime = mSimpleDateFormat.format(c.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mDateTime;

	}

	/**
	 * 格式化生日类型
	 * 
	 * @param year
	 * @param monthOfYear
	 * @param dayOfMonth
	 * @return
	 */
	public static String getBirthday(int year, int monthOfYear, int dayOfMonth) {
		String birthday = null;
		monthOfYear++;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date data = sdf.parse(year + "-" + monthOfYear + "-" + dayOfMonth);
			birthday = sdf.format(data);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return birthday;
	}

	/**
	 * 描述：计算年龄.
	 * 
	 * @param birthday
	 *            第一个时间的毫秒表示
	 * @return int 年龄
	 */
	public static int getOffectAge(String birthday) {
		long nowTime = System.currentTimeMillis();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		long birthdayTime = 0;
		try {
			birthdayTime = sdf.parse(birthday).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTimeInMillis(nowTime);
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTimeInMillis(birthdayTime);

		int y1 = calendar1.get(Calendar.YEAR);
		int y2 = calendar2.get(Calendar.YEAR);
		int age = y1 - y2;
		if (age < 0) {
			age = 0;
		}
		return age;
	}

	/**
	 * 描述：计算两个日期所差的天数.
	 * 
	 * @param date1
	 *            第一个时间的毫秒表示
	 * @param date2
	 *            第二个时间的毫秒表示
	 * @return int 所差的天数
	 */
	public static int getOffectDay(long date1, long date2) {
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTimeInMillis(date1);
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTimeInMillis(date2);
		// 先判断是否同年
		int y1 = calendar1.get(Calendar.YEAR);
		int y2 = calendar2.get(Calendar.YEAR);
		int d1 = calendar1.get(Calendar.DAY_OF_YEAR);
		int d2 = calendar2.get(Calendar.DAY_OF_YEAR);
		int maxDays = 0;
		int day = 0;
		if (y1 - y2 > 0) {
			maxDays = calendar2.getActualMaximum(Calendar.DAY_OF_YEAR);
			day = d1 - d2 + maxDays;
		} else if (y1 - y2 < 0) {
			maxDays = calendar1.getActualMaximum(Calendar.DAY_OF_YEAR);
			day = d1 - d2 - maxDays;
		} else {
			day = d1 - d2;
		}
		return day;
	}

	/**
	 * 描述：计算两个日期所差的天数.
	 *
	 * @return int 所差的天数
	 */
	public static String getOffectDayStr(String date) {
		try {
			String result = "";
			Calendar calendar1 = Calendar.getInstance();
			calendar1.setTimeInMillis(System.currentTimeMillis());

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar calendar2 = Calendar.getInstance();
			calendar2.setTimeInMillis(sdf.parse(date).getTime());

			// 先判断是否同年
			int y1 = calendar1.get(Calendar.YEAR);
			int y2 = calendar2.get(Calendar.YEAR);
			int d1 = calendar1.get(Calendar.DAY_OF_YEAR);
			int d2 = calendar2.get(Calendar.DAY_OF_YEAR);
			int maxDays = 0;
			int day = 0;
			if (y1 - y2 > 0) {
				maxDays = calendar2.getActualMaximum(Calendar.DAY_OF_YEAR);
				day = d1 - d2 + maxDays;
			} else if (y1 - y2 < 0) {
				maxDays = calendar1.getActualMaximum(Calendar.DAY_OF_YEAR);
				day = d1 - d2 - maxDays;
			} else {
				day = d1 - d2;
			}
			if (day == 0) {
				result = "今天";
			} else if (day > 0 && day < 7) {
				result = "一周前";
			} else if (day > 7 && day < 30) {
				result = "一个月前";
			} else {
				result = "一年前";
			}
			return result;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static int getOffectDayInt(String date) {
		try {
			Calendar calendar1 = Calendar.getInstance();
			calendar1.setTimeInMillis(System.currentTimeMillis());

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar calendar2 = Calendar.getInstance();
			calendar2.setTimeInMillis(sdf.parse(date).getTime());

			// 先判断是否同年
			int y1 = calendar1.get(Calendar.YEAR);
			int y2 = calendar2.get(Calendar.YEAR);
			int d1 = calendar1.get(Calendar.DAY_OF_YEAR);
			int d2 = calendar2.get(Calendar.DAY_OF_YEAR);
			int maxDays = 0;
			int day = 0;
			if (y1 - y2 > 0) {
				maxDays = calendar2.getActualMaximum(Calendar.DAY_OF_YEAR);
				day = d1 - d2 + maxDays;
			} else if (y1 - y2 < 0) {
				maxDays = calendar1.getActualMaximum(Calendar.DAY_OF_YEAR);
				day = d1 - d2 - maxDays;
			} else {
				day = d1 - d2;
			}

			return day;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 描述：计算两个日期所差的小时数.
	 * 
	 * @param date1
	 *            第一个时间的毫秒表示
	 * @param date2
	 *            第二个时间的毫秒表示
	 * @return int 所差的小时数
	 */
	public static int getOffectHour(long date1, long date2) {
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTimeInMillis(date1);
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTimeInMillis(date2);
		int h1 = calendar1.get(Calendar.HOUR_OF_DAY);
		int h2 = calendar2.get(Calendar.HOUR_OF_DAY);
		int h = 0;
		int day = getOffectDay(date1, date2);
		h = h1 - h2 + day * 24;
		return h;
	}

	/**
	 * 描述：计算两个日期所差的分钟数.
	 * 
	 * @param date1
	 *            第一个时间的毫秒表示
	 * @param date2
	 *            第二个时间的毫秒表示
	 * @return int 所差的分钟数
	 */
	public static int getOffectMinutes(long date1, long date2) {
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTimeInMillis(date1);
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTimeInMillis(date2);
		int m1 = calendar1.get(Calendar.MINUTE);
		int m2 = calendar2.get(Calendar.MINUTE);
		int h = getOffectHour(date1, date2);
		int m = 0;
		m = m1 - m2 + h * 60;
		return m;
	}

	/**
	 * 描述：获取本周一.
	 * 
	 * @param format
	 *            the format
	 * @return String String类型日期时间
	 */
	public static String getFirstDayOfWeek(String format) {
		return getDayOfWeek(format, Calendar.MONDAY);
	}

	/**
	 * 描述：获取本周日.
	 * 
	 * @param format
	 *            the format
	 * @return String String类型日期时间
	 */
	public static String getLastDayOfWeek(String format) {
		return getDayOfWeek(format, Calendar.SUNDAY);
	}

	/**
	 * 描述：获取本周的某一天.
	 * 
	 * @param format
	 *            the format
	 * @param calendarField
	 *            the calendar field
	 * @return String String类型日期时间
	 */
	private static String getDayOfWeek(String format, int calendarField) {
		String strDate = null;
		try {
			Calendar c = new GregorianCalendar();
			SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
			int week = c.get(Calendar.DAY_OF_WEEK);
			if (week == calendarField) {
				strDate = mSimpleDateFormat.format(c.getTime());
			} else {
				int offectDay = calendarField - week;
				if (calendarField == Calendar.SUNDAY) {
					offectDay = 7 - Math.abs(offectDay);
				}
				c.add(Calendar.DATE, offectDay);
				strDate = mSimpleDateFormat.format(c.getTime());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strDate;
	}

	/**
	 * 获取时间戳， 当月的日期， 日期
	 * @param milliseconds
	 * @return
	 */
	public static int getDayOfMonth(long milliseconds){

		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(milliseconds);
		return c.get(Calendar.DAY_OF_MONTH);

	}

	/**
	 * 描述：获取本月第一天.
	 * 
	 * @param format
	 *            the format
	 * @return String String类型日期时间
	 */
	public static String getFirstDayOfMonth(String format) {
		String strDate = null;
		try {
			Calendar c = new GregorianCalendar();
			SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
			// 当前月的第一天
			c.set(GregorianCalendar.DAY_OF_MONTH, 1);
			strDate = mSimpleDateFormat.format(c.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strDate;

	}

	/**
	 * 描述：获取本月最后一天.
	 * 
	 * @param format
	 *            the format
	 * @return String String类型日期时间
	 */
	public static String getLastDayOfMonth(String format) {
		String strDate = null;
		try {
			Calendar c = new GregorianCalendar();
			SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
			// 当前月的最后一天
			c.set(Calendar.DATE, 1);
			c.roll(Calendar.DATE, -1);
			strDate = mSimpleDateFormat.format(c.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strDate;
	}

	/**
	 * 描述：获取表示当前日期的0点时间毫秒数.
	 * 
	 * @return the first time of day
	 */
	public static long getFirstTimeOfDay() {
		Date date = null;
		try {
			String currentDate = getCurrentDate(dateFormatYMD);
			date = getDateByFormat(currentDate + " 00:00:00", dateFormatYMDHMS);
			return date.getTime();
		} catch (Exception e) {
		}
		return -1;
	}

	/**
	 * 描述：获取表示当前日期24点时间毫秒数.
	 * 
	 * @return the last time of day
	 */
	public static long getLastTimeOfDay() {
		Date date = null;
		try {
			String currentDate = getCurrentDate(dateFormatYMD);
			date = getDateByFormat(currentDate + " 24:00:00", dateFormatYMDHMS);
			return date.getTime();
		} catch (Exception e) {
		}
		return -1;
	}

	/**
	 * 描述：判断是否是闰年()
	 * <p>
	 * (year能被4整除 并且 不能被100整除) 或者 year能被400整除,则该年为闰年.
	 * 
	 * @param year
	 *            年代（如2012）
	 * @return boolean 是否为闰年
	 */
	public static boolean isLeapYear(int year) {
		if ((year % 4 == 0 && year % 400 != 0) || year % 400 == 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 描述：根据时间返回几天前或几分钟的描述.
	 * 
	 * @param strDate
	 *            the str date
	 * @return the string
	 */
	public static String formatDateStr2Desc(String strDate, String outFormat) {

		DateFormat df = new SimpleDateFormat(dateFormatYMDHMS);
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c2.setTime(df.parse(strDate));
			c1.setTime(new Date());
			int d = getOffectDay(c1.getTimeInMillis(), c2.getTimeInMillis());
			if (d == 0) {
				int h = getOffectHour(c1.getTimeInMillis(),
						c2.getTimeInMillis());
				if (h > 0) {
					return h + "小时前";
				} else if (h < 0) {
					return Math.abs(h) + "小时后";
				} else if (h == 0) {
					int m = getOffectMinutes(c1.getTimeInMillis(),
							c2.getTimeInMillis());
					if (m > 0) {
						return m + "分钟前";
					} else if (m < 0) {
						return Math.abs(m) + "分钟后";
					} else {
						return "刚刚";
					}
				}
			} else if (d > 0) {
				if (d == 1) {
					return "昨天";
				} else if (d == 2) {
					return "前天";
				}
			} else if (d < 0) {
				if (d == -1) {
					return "明天";
				} else if (d == -2) {
					return "后天";
				}
				return Math.abs(d) + "天后";
			}

			String out = getStringByFormat(strDate, outFormat);
			if (!TextUtils.isEmpty(out)) {
				return out;
			}
		} catch (Exception e) {
		}

		return strDate;
	}

	/**
	 * 取指定日期为星期几
	 * 
	 * @param strDate
	 *            指定日期
	 * @param inFormat
	 *            指定日期格式
	 * @return String 星期几
	 */
	public static String getWeekNumber(String strDate, String inFormat) {
		String week = "星期日";
		Calendar calendar = new GregorianCalendar();
		DateFormat df = new SimpleDateFormat(inFormat);
		try {
			calendar.setTime(df.parse(strDate));
		} catch (Exception e) {
			return "错误";
		}
		int intTemp = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		switch (intTemp) {
		case 0:
			week = "星期日";
			break;
		case 1:
			week = "星期一";
			break;
		case 2:
			week = "星期二";
			break;
		case 3:
			week = "星期三";
			break;
		case 4:
			week = "星期四";
			break;
		case 5:
			week = "星期五";
			break;
		case 6:
			week = "星期六";
			break;
		}
		return week;
	}

	/**
	 * 获取随机订单号
	 * 
	 * @return
	 */
	public static String getOrderId() {
		String time = new SimpleDateFormat("yyMMddHHmmss").format(new Date());
		Double dRandom = new Double(10000 * Math.random());
		Integer iRandom = new Integer(dRandom.intValue());
		return time + iRandom;
	}

	/**
	 * The main method.
	 * 
	 * @param args
	 *            the arguments
	 */
	public static void main(String[] args) {
		System.out.println(formatDateStr2Desc("2012-3-2 12:2:20",
				"MM月dd日  HH:mm"));
	}

	/**
	 * 格式化事件并转换成字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String formatDate2String(Date date,String format){

		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);

	}

	public static String[] weekFullNameDays;
	/**
	 * 获取当前日期是星期几<br>
	 *
	 * @param milliseconds
	 * @return 当前日期是星期几
	 */
	public static String getWeekOfDate(long milliseconds) {
//		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "今天"};
//		String[] weekDays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Today"};

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(milliseconds);

		if(weekFullNameDays == null){
			weekFullNameDays = BaseApplication.getInstance().getResources().getStringArray(R.array.week_full_name);
		}

		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;

		return weekFullNameDays[w];
	}

	private static String[] weekDays;
	/**
	 * 获取当前日期是星期几<br>
	 *
	 * @param cal
	 * @param isShowToday
	 * @return 当前日期是星期几
	 */
	public static String getWeekOfDate(Calendar cal, boolean isShowToday) {
//		String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "今天"};
//		String[] weekDays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Today"};

		if(weekDays == null){
			weekDays = BaseApplication.getInstance().getResources().getStringArray(R.array.week);
		}

		if(isShowToday){

			if(formatDate2String(cal.getTime(), dateFormatYMD).equals(formatDate2String(Calendar.getInstance().getTime(), dateFormatYMD))){

				return weekDays[7];
			}

		}

		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;

		return weekDays[w];
	}

	/** 
	 * 日历转日期 
	 * @param calendar 
	 * @return Date 
	 */  
	public static Date CalendarToDate(Calendar calendar) {  
		Date  date= calendar.getTime();     
		return date;  
	}


	/*
	 * 很无聊的方法
	 */
	public static Boolean infoTime(String time){

		//获取当前时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date dt=new Date();
		String [] qq = sdf.format(dt).split("-");
		int n = Integer.parseInt(qq[0]);
		int y = Integer.parseInt(qq[1]);
		//传进来的时间
		String [] q = time.split("-");
		String w = q[0];
		int nian = Integer.parseInt(q[0]);
		int yue = Integer.parseInt(q[1]);
		if((nian*12+yue)-(n*12+y)==0 || (nian*12+yue)-(n*12+y)==1){
			return true;
			
		}else {
			
			return false;
		}

	}

	public static String getTime(String user_time) {
		  String re_time = null;
		  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		  Date d;
		  try {
		   d = sdf.parse(user_time);
		   long l = d.getTime();
		   String str = String.valueOf(l);
		   re_time = str.substring(0, 10);
		  } catch (ParseException e) {
		   e.printStackTrace();
		  }
		  return re_time;
		 }
}
